package source;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.ParallelSourceFunction;

/**
 * 自定义带有并行度的Source
 */
public class CustomSource2 {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        DataStreamSource<Integer> data = env.addSource(new CustomParSource());
        data.print();

        env.execute("CustomSource2");
    }

    static class CustomParSource implements ParallelSourceFunction<Integer> {

        private static boolean flag = true;

        // 产生数据的方法
        @Override
        public void run(SourceContext<Integer> sourceContext) throws Exception {
            // 生成数据
            int i = 1;
            while (flag) {
                sourceContext.collect(i++);
                Thread.sleep(1000);
            }
        }

        @Override
        public void cancel() {

        }
    }
}
